import { ecsSubscriberInfoApi } from '@/api/mock/ecssubscriberinfo'

export default ({ request, serviceForMock, mock, faker }) => {
  
  // Mock 租户信息管理接口
  mock.onGet('/ecs/ecssubscriberinfo/list').reply(config => {
    const params = new URLSearchParams(config.url.split('?')[1] || '')
    const page = parseInt(params.get('page')) || 1
    const limit = parseInt(params.get('limit')) || 10
    const paramKey = params.get('paramKey') || ''
    const customerId = params.get('customerId') || ''

    return ecsSubscriberInfoApi.getSubscriberList({ page, limit, paramKey, customerId })
      .then(response => [200, response])
  })

  // Mock 获取证件类型接口
  mock.onGet('/sys/sysstaticdata/getList').reply(config => {
    const params = new URLSearchParams(config.url.split('?')[1] || '')
    const code = params.get('code')
    
    if (code === 'CARD_TYPE') {
      return ecsSubscriberInfoApi.getCardTypes()
        .then(response => [200, response])
    }
    
    return [200, { code: 0, msg: 'success', page: [] }]
  })

  // Mock 保存租户信息接口
  mock.onPost('/ecs/ecssubscriberinfo/save').reply(config => {
    const data = JSON.parse(config.data)
    return ecsSubscriberInfoApi.saveSubscriber(data)
      .then(response => [200, response])
  })

  // Mock 删除租户信息接口
  mock.onPost('/ecs/ecssubscriberinfo/delete').reply(config => {
    const ids = JSON.parse(config.data)
    return ecsSubscriberInfoApi.deleteSubscriber(ids)
      .then(response => [200, response])
  })

  // Mock 获取租户详情接口
  mock.onGet(/\/ecs\/ecssubscriberinfo\/info\/\d+/).reply(config => {
    const id = config.url.split('/').pop()
    return ecsSubscriberInfoApi.getSubscriberInfo(id)
      .then(response => [200, response])
  })

  return {
    // 获取租户信息列表
    ecs_subscriber_list(params = {}) {
      return serviceForMock({
        url: '/ecs/ecssubscriberinfo/list',
        method: 'get',
        params
      })
    },

    // 获取证件类型
    sys_static_data_list(params = {}) {
      return serviceForMock({
        url: '/sys/sysstaticdata/getList',
        method: 'get',
        params
      })
    },

    // 保存租户信息
    ecs_subscriber_save(data = {}) {
      return serviceForMock({
        url: '/ecs/ecssubscriberinfo/save',
        method: 'post',
        data
      })
    },

    // 删除租户信息
    ecs_subscriber_delete(data = {}) {
      return serviceForMock({
        url: '/ecs/ecssubscriberinfo/delete',
        method: 'post',
        data
      })
    },

    // 获取租户详情
    ecs_subscriber_info(id) {
      return serviceForMock({
        url: `/ecs/ecssubscriberinfo/info/${id}`,
        method: 'get'
      })
    }
  }
}

